package quesiton;

import login.dbhelper;
import quesiton.SettingOp;
import quesiton.methrodConst;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class fillMath {
    protected static dbhelper db=new dbhelper();
    protected static ArrayList questionList = new ArrayList();//题目控件列表
    protected static ArrayList anserList = new ArrayList();//答案控件列表
    protected static ArrayList judgeList = new ArrayList();//对错判断控件列表
    protected static ArrayList<Float> anserCo = new ArrayList<>();//正确的答案列表
    protected static ArrayList<String> questionStr = new ArrayList<>();//正确的答案列表
    protected static int m_score = 0;//得分
    protected static int m_totalBlank = SettingOp.m_blankAmount;//填空题总数
    protected static JButton button_submit;
    protected static JButton button_redo;
    protected static JTextField textField_feedback1;
    protected static JTextField textField_feedback2;
    protected static JLabel hint;
    // protected static JButton button_next;

    public static void doFillBlank() {
        JFrame f = new JFrame("四则考试系统");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().setLayout(null);
        f.setBounds(50, 50, 856, 616);
        hint = new JLabel("当前进度：" + methrodConst.m_anseredAmount + "/" + SettingOp.m_blankAmount);
        hint.setFont(new Font("", Font.BOLD, 29));
        hint.setBounds(280, 20, 280, 50);
        f.getContentPane().add(hint);
        methrodConst.generateQuestion(f);//产生答题主体布局
        //提交并判定分数
        button_submit = new JButton("提交");
        button_submit.setFont(new Font("宋体", Font.PLAIN, 30));
        button_submit.setBounds(86, 425, 102, 48);
        m_score = 0;
        button_submit.addActionListener(new ActionListener() {
            //对了则分数加一分且判断框中显示“√”，错了则不加分且判断框中显示“×”
            @Override
            public void actionPerformed(ActionEvent e) {
                methrodConst.m_anseredAmount += methrodConst.m_currentAmount;
                hint.setText("当前进度：" + methrodConst.m_anseredAmount + "/" + SettingOp.m_blankAmount);
                for (int i = 0; i < methrodConst.m_anseredAmount; i++) {
                    JTextField anser = (JTextField) anserList.get(i);
                    JTextField judge = (JTextField) judgeList.get(i);
                    int anser_q = -1212;
                    if (!anser.getText().equals("")) {
                        anser_q = Integer.parseInt(anser.getText());
                    }
                    //判断控件
                    if (anser_q == anserCo.get(i)) {
                        m_score += 10;
                        judge.setText("√");
                    } else {
                        judge.setText("X");
                    }
                }
                if (methrodConst.m_anseredAmount < SettingOp.m_blankAmount) {
                    textField_feedback2.setText("提交成功 2s后进入下一页");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(2000);
                          /*      for (Object o : questionList) {
                                    JTextField s=(JTextField)o;
                                    ((JTextField) o).setVisible(false);
                                }*/
                                methrodConst.generateQuestion(f);//产生答题主体布局
                                textField_feedback2.setText("此页答题完成后点击提交哦！");
                            } catch (InterruptedException interruptedException) {
                                interruptedException.printStackTrace();
                            }
                        }
                    }).start();
                } else {
                    float min = (float) m_totalBlank * 100;
                    float temp1 = (float) m_score / min;
                    db.submit(questionStr,anserCo);
                    System.out.println("当前得分()：" + m_score + "\n" + "分母：" + min + " 得分率：" + temp1 * 1000 + "%");
                    textField_feedback2.setText("答题完成  " + " 正确率： " + temp1 * 1000 + "%");
                }
            }
        });
        f.getContentPane().add(button_submit);

        //重写操作
        button_redo = new JButton("重做");
        button_redo.setFont(new Font("宋体", Font.PLAIN, 30));
        button_redo.setBounds(606, 425, 102, 48);
        button_redo.addActionListener(new ActionListener() {

            @Override  //所有答题区域和判断区域清空
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < anserList.size(); i++) {
                    //回答清空
                    JTextField answer = (JTextField) anserList.get(i);
                    answer.setText("");
                    //对错清空
                    JTextField judge = (JTextField) judgeList.get(i);
                    judge.setText("");
                }
                textField_feedback2.setText("");
                m_score = 0;  //得分归零
            }
        });
        f.getContentPane().add(button_redo);

        textField_feedback2 = new JTextField();
        textField_feedback2.setFont(new Font("宋体", Font.PLAIN, 25));
        textField_feedback2.setBounds(435, 485, 360, 64);
        textField_feedback2.setEditable(false);
        textField_feedback2.setText("此页答题完成后点击提交哦！");
        f.getContentPane().add(textField_feedback2);
        textField_feedback2.setColumns(1);
        textField_feedback1 = new JTextField();
        textField_feedback1.setFont(new Font("宋体", Font.PLAIN, 30));
        textField_feedback1.setBounds(35, 485, 360, 64);
        textField_feedback1.setEditable(false);
        f.getContentPane().add(textField_feedback1);
        textField_feedback1.setColumns(1);
        //点击进入选择题
     /*   button_next = new JButton("下一页");
        button_next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                //JOptionPane.showMessageDialog(null, "注册成功！继续登录吧", "提示窗口", JOptionPane.INFORMATION_MESSAGE);

            }
        });
        button_next.setFont(new Font("宋体", Font.PLAIN, 30));
        button_next.setBounds(316, 425, 141, 48);*/

        final long end = System.currentTimeMillis() + 20 * 1000 * 60;
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            //获取剩余的倒计时长
            public void run() {
                long sub = end - System.currentTimeMillis();
                if (sub < 0) {
                    return;
                }
                updateTimer(sub);
            }

        }, 0, 1000);

        //f.getContentPane().add(button_next);
        SettingOp.setBackground(f);
        f.setVisible(true);
    }

    //显示成HH：mm：ss样的格式，刷新剩余的时长
    private static void updateTimer(long sub) {
        int h = (int) (sub / 1000 / 60 / 60);
        int m = (int) (sub / 1000 / 60 % 60);
        int s = (int) (sub / 1000 % 60);
        if (h==0&&m==0&&s==0)
        {
            methrodConst.m_anseredAmount += methrodConst.m_currentAmount;
            hint.setText("当前进度：" + methrodConst.m_anseredAmount + "/" + SettingOp.m_blankAmount);
            for (int i = 0; i < methrodConst.m_anseredAmount; i++) {
                JTextField anser = (JTextField) anserList.get(i);
                JTextField judge = (JTextField) judgeList.get(i);
                int anser_q = -1212;
                if (!anser.getText().equals("")) {
                    anser_q = Integer.parseInt(anser.getText());
                }
                //判断控件
                if (anser_q == anserCo.get(i)) {
                    m_score += 10;
                    judge.setText("√");
                } else {
                    judge.setText("X");
                }
            }
            if (methrodConst.m_anseredAmount < SettingOp.m_blankAmount) {
                textField_feedback2.setText("提交成功 2s后进入下一页");
            } else {
                float min = (float) m_totalBlank * 100;
                float temp1 = (float) m_score / min;
                db.submit(questionStr,anserCo);
                System.out.println("当前得分()：" + m_score + "\n" + "分母：" + min + " 得分率：" + temp1 * 1000 + "%");
                textField_feedback2.setText("答题完成  " + " 正确率： " + temp1 * 1000 + "%");
            }
        }
        String str = h + ":" + m + ":" + s;
        //将String类型转换成Date类型的格式
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        Font font = new Font("Default", Font.PLAIN, 40);
        textField_feedback1.setFont(font);
        try {
            date = sdf.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //将Date类型的数设置成想要显示的时间格式,并写入JLable中
        textField_feedback1.setText("倒计时：" + sdf.format(date));
    }

    public static void main(String[] args) {
        doFillBlank();
    }


}
